﻿using System.ServiceModel.Syndication;
using System.Web.Mvc;
using System.Xml;

namespace Sharp.Portal.Mvc.ActionResults
{
    /// <summary>
    /// ATOM是一种订阅网志的格式
    /// </summary>
    public class AtomActionResult : ActionResult
    {
        /// <summary>
        /// 版本设定
        /// </summary>
        public SyndicationFeed Feed { get; set; }

        /// <summary>
        /// ATOM是一种订阅网志的格式
        /// </summary>
        /// <param name="feed"></param>
        public AtomActionResult(SyndicationFeed feed)
        {
            this.Feed = feed;
        }

        /// <summary>
        /// 返回结果
        /// </summary>
        /// <param name="context"></param>
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/atom+xml";

            Atom10FeedFormatter formatter = new Atom10FeedFormatter(this.Feed);

            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                formatter.WriteTo(writer);
            }
        }
    }
}